﻿Imports TepcelDominio
Imports TepcelBL
Public Class frmEmpresa
    Dim err As New System.Windows.Forms.ErrorProvider
    Dim listaEmpresa As New List(Of Empresa)
    Dim EmpresaBL As New EmpresaBL

    Private Sub frmEmpresa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call CargarGrid()
        txtBuscar.Focus()
    End Sub

    Private Sub clearError()
        err.SetError(txtRucEmpresa, "")
        err.SetError(txtNombreEmpresa, "")
    End Sub

    Private Sub CargarGrid()
        DGVLista.Rows.Clear()
        listaEmpresa = EmpresaBL.GetAllFromEmpresa
        If listaEmpresa.Count = 0 Then
            btnModificar.Enabled = False
            btnEliminar.Enabled = False
        End If
        Dim i As Integer
        For i = 0 To listaEmpresa.Count - 1
            With listaEmpresa.Item(i)
                DGVLista.Rows.Add(i, .codigoEmpresa, .rucEmpresa, .nombreEmpresa)
            End With
        Next
    End Sub

    Private Sub nuevo()
        btnModificar.Enabled = False
        btnEliminar.Enabled = False
        btnCancelar.Enabled = True
        btnGuardar.Enabled = True
        btnNuevo.Enabled = False

        txtRucEmpresa.Enabled = True
        txtNombreEmpresa.Enabled = True

        DGVLista.Enabled = False

        txtRucEmpresa.Focus()
    End Sub

    Private Sub cancelar()
        btnModificar.Enabled = True
        btnEliminar.Enabled = True
        btnCancelar.Enabled = False
        btnGuardar.Enabled = False
        btnNuevo.Enabled = True

        txtRucEmpresa.Enabled = False
        txtNombreEmpresa.Enabled = False

        DGVLista.Enabled = True

        txtCodigoEmpresa.Text = ""
        txtRucEmpresa.Text = ""
        txtNombreEmpresa.Text = ""
    End Sub

    Private Sub txtBuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged
        If txtBuscar.Text.Trim.Length = 0 Then
            Call CargarGrid()
        Else
            DGVLista.Rows.Clear()
            listaEmpresa = EmpresaBL.SearchFromEmpresa(txtBuscar.Text.Trim)
            Dim i As Integer
            For i = 0 To listaEmpresa.Count - 1
                With listaEmpresa.Item(i)
                    DGVLista.Rows.Add(i, .codigoEmpresa, .rucEmpresa, .nombreEmpresa)
                End With
            Next
        End If
    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click
        Call nuevo()
        Call clearError()
        Dim item As Integer = DGVLista.Item(0, DGVLista.CurrentRow.Index).Value
        With listaEmpresa.Item(item)
            txtCodigoEmpresa.Text = .codigoEmpresa
            txtRucEmpresa.Text = .nombreEmpresa
            txtNombreEmpresa.Text = .nombreEmpresa
        End With

        txtRucEmpresa.Enabled = True
        txtNombreEmpresa.Enabled = True


    End Sub

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If txtRucEmpresa.Text.Trim.Length = 0 Then
            err.SetError(txtRucEmpresa, "Ingrese el RUC de la Empresa")
            Return
        Else
            err.SetError(txtRucEmpresa, "")
        End If

        If txtNombreEmpresa.Text.Trim.Length = 0 Then
            err.SetError(txtNombreEmpresa, "Ingrese el nombre de la Empresa")
            Return
        Else
            err.SetError(txtNombreEmpresa, "")
        End If

        If (txtCodigoEmpresa.Text.Trim.Length > 0) Then
            Dim empre As New TepcelDominio.Empresa(txtCodigoEmpresa.Text, txtNombreEmpresa.Text.Trim, txtRucEmpresa.Text.Trim)
            txtCodigoEmpresa.Text = EmpresaBL.NuevoEmpresa(empre)
        Else
            Dim empre As New TepcelDominio.Empresa("", txtNombreEmpresa.Text.Trim, txtRucEmpresa.Text.Trim)
            txtCodigoEmpresa.Text = EmpresaBL.NuevoEmpresa(empre)
        End If


        Call cancelar()


        Call CargarGrid()

    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        Dim result As DialogResult
        result = MessageBox.Show("confirma que desea Eliminar el Registro seleccionado?", "Eliminar", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
        If result = DialogResult.OK Then
            If EmpresaBL.DeleteEmpresa(listaEmpresa.Item(CInt(DGVLista.Item(0, DGVLista.CurrentRow.Index).Value)).codigoEmpresa) Then
                MessageBox.Show("Registro Eliminado Correctamente", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Call CargarGrid()
            Else
                MessageBox.Show("No se elimino el registro seleccionado", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Call cancelar()
        Call clearError()

        txtBuscar.Focus()

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        Call nuevo()
        Call clearError()
    End Sub

    
End Class